package com.zb.jsp.service.active;

import java.util.List;

import com.zb.jsp.dao.active.TblActiveDao;
import com.zb.jsp.pojo.active.TblActive;
import com.zb.jsp.utils.PropertiesCopyUtil;
import com.zb.jsp.vo.active.ActiveVo;

public class ActiveService {

	
	TblActiveDao tblActiveDao = new TblActiveDao();
	
	/**
	   * 将活动数据保存到到活动表中
	   * 
	   * @param tblActive
	   *          活动信息
	   * @param  String[]选择参加活动的广告id   
	   */
	  public void addTblActive(ActiveVo activeVo, String[] rightAdvertIdList) {
		  TblActive tblActive = new TblActive();
		  // 调用外部自定义方法把Vo的值赋值给POJO
		  PropertiesCopyUtil.copy(tblActive,activeVo);	
		  tblActiveDao.addTblActive(tblActive, rightAdvertIdList);
	  }
	  
	  /**
		 * 根据活动状态、活动开始或结束时间或活动名称查询活动信息列表
		 * 
		 * @param activeVoQry
		 *            活动状态、活动开始或结束时间或活动名称
		 * @return advertVoList查询的活动信息
		 */
		public List<ActiveVo> findActiveVoListByAdctiveVoQry(ActiveVo activeVoQry) {

			//获取数据库中的记录数
			int activeCounts = tblActiveDao.findActiveVoCountByActiveVoQry(activeVoQry);
			if(activeCounts == 0){
				return null;
			}
			 // 取得总共有多少页(【总的记录数】/【每一页显示多少条记录】+（【总的记录数】%【每一页显示多少条记录】==0?0:1))
			int totalPages = (activeCounts/activeVoQry.getNumPerPage()) + (activeCounts % activeVoQry.getNumPerPage() == 0 ? 0:1);
			activeVoQry.setTotalPages(totalPages);
			//判断当前页是否为第一页
			if(activeVoQry.getCurPage() == null || activeVoQry.getCurPage() == 0){
				activeVoQry.setCurPage(1);
			}
			//将查询结果保存到volist中返回页面
			return tblActiveDao.findActiveVoListByAdctiveVoQry(activeVoQry);
		}

		/**
		 * 根据页面获取到的id号删除数据库信息
		 * 
		 * @param ids
		 *            拼接好 的id 号
		 */
		public void delActives(String ids) {

			// 判断传入的selectId是否为空
			if ("".equals(ids)||ids == null) {
				System.out.println("请选择要删除的记录");
				return ;
			}
			tblActiveDao.delActives(ids);
		}
		

		/**	
		 * 根据id查询数据库信息并保存在AdvertVo对象中
		 * @param id 页面传回的id值
		 * @return 返回查询到的vo对象
		 */
		public ActiveVo findTblActiveById(int id) {
			ActiveVo activeVo = new ActiveVo();

			TblActive tblActive = tblActiveDao
					.findTblActiveById(id);
			// 把POJO的值赋值给VO
			PropertiesCopyUtil.copy(activeVo, tblActive);

			return activeVo;
		}
		
		public void uptTblActive(ActiveVo activeVo, String[] rightAdvertIdList){
			TblActive tblActive = new TblActive();
			// 调用外部自定义方法把Vo的值赋值给POJO
			PropertiesCopyUtil.copy(tblActive, activeVo);
			tblActiveDao.uptTblActive(tblActive, rightAdvertIdList);
		}
}
